//	Lexeme.java

import java.io.*;

/** 
Description: Lexeme class for interpreted language

@author Teresa Cole
@author Samuel Jenkins
@version CS354 Fall 2005 - modified Fall 2008
 */
public class Lexeme implements Token
{

    private int token;
    private float floatValue;
    private String stringValue;

    /**  Creates a Lexeme of UNKNOWN type
     */
    public Lexeme()
    {
        token = UNKNOWN;
    }

    /** Creates a Lexeme which has no data associated with it
    
    @param tokenName is the token for the Lexeme
     */
    public Lexeme(int tokenName)
    {
        token = tokenName;
    }

    /** Creates a Lexeme which has a float value associated with it.
    
    @param tokenName is the token for the Lexeme
    @param number is the numeric value to be stored with the Lexeme
     */
    public Lexeme(int tokenName, float number)
    {
        token = tokenName;
        floatValue = number;
    }

    /** Creates a Lexeme which has String data associated with it.  
    
    @param tokenName is the token for the Lexeme
    @param str is the String to be stored with the Lexeme
     */
    public Lexeme(int tokenName, String str)
    {
        token = tokenName;
        stringValue = str;
    }

    /** Returns the token for the Lexeme
    
    @return the token for the Lexeme
     */
    public int getType()
    {
        return token;
    }

    /** Returns the integer value associated with the Lexeme.  The Lexeme should
     * have type NUMBER.
    
    @return the integer value of the Lexeme 
     */
    public float getValue()
    {
        return floatValue;
    }

    /** Returns the String value associated with the Lexeme.  The Lexeme should
     * have type VAR.
    
    @return the string value of the Lexeme 
     */
    public String getStringValue()
    {
        return stringValue;
    }

    /**  Modifies the token for the Lexeme
    
    @param tokenName is the token for the Lexeme
     */
    public void setType(int tokenName)
    {
        token = tokenName;
    }

    /**  Prints the Token and a value if appropriate. 
    
    @param out is the destination of the output
     */
    public void print(PrintStream out)
    {
        switch (token)
        {
            case NUMBER:
                out.println("NUMBER : " + floatValue);
                break;
            case VAR:
                out.println("VAR : " + stringValue);
                break;
            case UNKNOWN:
                out.println("Unknown character");
                break;
            case ERROR:
                out.println("ERROR");
                break;
            default:
                out.println(lexString[token]);
        }
    }

    /** The toString method is used for printing out code
    
    @return a String giving the Lexeme as it appears in the code
     */
    public String toString()
    {
        String result = "";
        switch (token)
        {
            case NUMBER:
                result += floatValue;
                break;
            case VAR:
                result += stringValue;
                break;
            case UNKNOWN:
                result += "UNKNOWN";
                break;
            case ERROR:
                result += "ERROR";
                break;
            default:
                result += " " + lexNames[token];
        }
        return result;
    }

    /** Prints a representation of a parse tree using preorder traversal.
     * Indentation is used to keep track of the levels.
     */
    public void printTree(PrintStream out, int level)
    {
        if (level > 0)
        {
            out.print(repeat(' ', level) + "|" + repeat('-', level));
        }
        print(out);
        if (left != null)
        {
            left.printTree(out, level + 1);
        }
        if (right != null)
        {
            right.printTree(out, level + 1);
        }
    }

    /** Builds up a string of repeated characters for indenting the nodes of a
     * parse tree.
    
    @param ch is the character to be repeated
    @param count is the number of times ch should appear in the string
    @return a String containing ch count times
     */
    private String repeat(char ch, int count)
    {
        StringBuffer result = new StringBuffer(count);
        for (int i = 0; i < count; i++)
        {
            result.append(ch);
        }
        return result.toString();
    }
    /*--------------------------------------------------------------
     *
     *  Class members needed for building a parse tree
     */
    private Lexeme left,  right;

    /**
     */
    public Lexeme getLeft()
    {
        return left;
    }

    /**
     */
    public void setLeft(Lexeme child)
    {
        left = child;
    }

    /**
     */
    public Lexeme getRight()
    {
        return right;
    }

    /**
     */
    public void setRight(Lexeme child)
    {
        right = child;
    }
}
